<template>
  <view class="shop-detail">
    <!-- 商家基本信息 -->
    <view class="shop-header">
      <image class="shop-logo" :src="shop.logo" mode="aspectFill"></image>
      <view class="shop-info">
        <view class="shop-name">{{ shop.name }}</view>
        <view class="shop-rating">
          <uni-rate :value="shop.rating" readonly size="14" />
          <text>{{ shop.rating }}</text>
        </view>
        <view class="shop-meta">
          <text>月售: {{ shop.monthlySales }}单</text>
        </view>
      </view>
    </view>

    <!-- 商家菜单 -->
    <view class="menu-section">
      <view class="section-title">商家菜单</view>
      <view v-for="category in shop.menu" :key="category.id" class="menu-category">
        <view class="category-title">{{ category.name }}</view>
        <view v-for="item in category.items" :key="item.id" class="menu-item" @click="addToCart(item)">
          <image :src="item.image" class="item-image" />
          <view class="item-info">
            <view class="item-name">{{ item.name }}</view>
            <view class="item-desc">{{ item.description }}</view>
            <view class="item-price">¥{{ item.price }}</view>
          </view>
        </view>
      </view>
      <view style="height: 50px;"></view>
    </view>

    <!-- 底部购物车 -->
    <view class="cart-bar">
      <view class="cart-icon" @click="showCartPopup">
        <uni-icons type="cart" size="30" color="#333"></uni-icons>
        <view class="cart-badge" v-if="cartCount > 0">{{ cartCount }}</view>
      </view>
      <view class="cart-total">
        <text>总计: ¥{{ getTotalPrice.toFixed(2) }}</text>
      </view>
      <button class="checkout-btn" @click="checkout">去结算</button>
    </view>

    <!-- 购物车弹窗 -->
    <uni-popup ref="cartPopup" type="bottom">
      <view class="cart-popup">
        <view class="popup-header">
          <text>购物车</text>
          <uni-icons type="close" size="20" @click="hideCartPopup"></uni-icons>
        </view>
        <scroll-view class="cart-items" scroll-y>
          <view v-for="item in cartItems" :key="item.id" class="cart-item">
            <view class="item-info">
              <text class="item-name">{{ item.name }}</text>
              <text class="item-price">¥{{ item.price.toFixed(2) }}</text>
            </view>
            <view class="item-actions">
              <button class="btn-decrease" @click="decreaseItem(item)">-</button>
              <text class="item-quantity">{{ item.quantity }}</text>
              <button class="btn-increase" @click="increaseItem(item)">+</button>
            </view>
          </view>
        </scroll-view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
  import {
    useShopStore
  } from '@/store/shopStore'

  export default {
    data() {
      return {
        shop: {},
        cartItems: [],
        totalPrice: 0,
        shopStore: useShopStore()
      }
    },
    computed: {
      cartCount() {
        return this.cartItems.reduce((total, item) => total + item.quantity, 0)
      },
      getTotalPrice() {
        const price = this.cartItems.reduce((total, item) => total + (item.price * item.quantity), 0)
        this.totalPrice = price

        return price
      }
    },
    onLoad(options) {
      this.$nextTick(async () => {
        try {
          this.shop = await this.shopStore.getShopById(options.id) || {}
          uni.setNavigationBarTitle({
            title: options.name || '商家详情'
          })
        } catch (error) {
          console.error('获取店铺信息失败:', error);
          uni.showToast({
            title: '获取店铺信息失败，请重试',
            icon: 'none'
          });
          uni.navigateBack();
        }
      })
    },
    methods: {
      addToCart(item) {
        const existingItem = this.cartItems.find(i => i.id === item.id)
        if (existingItem) {
          existingItem.quantity++
        } else {
          this.cartItems.push({
            ...item,
            quantity: 1
          })
        }
        uni.showToast({
          title: `已添加 ${item.name}`,
          icon: 'success'
        })
      },
      increaseItem(item) {
        item.quantity++
      },
      decreaseItem(item) {
        if (item.quantity > 1) {
          item.quantity--
        } else {
          this.cartItems = this.cartItems.filter(i => i.id !== item.id)
        }
      },
      showCartPopup() {
        this.$refs.cartPopup.open()
      },
      hideCartPopup() {
        this.$refs.cartPopup.close()
      },
      checkout() {
        if (this.cartCount === 0) {
          uni.showToast({
            title: '请先添加商品',
            icon: 'none'
          })
          return
        }
        uni.navigateTo({
          url: '/pages/order/checkout?price=' + this.totalPrice
        })
      }
    }
  }
</script>

<style scoped>
  /* 原有样式保持不变 */
  .shop-header {
    display: flex;
    padding: 15px;
    background: #fff;
  }

  .shop-header {
    display: flex;
    padding: 15px;
    background: #fff;
  }

  .shop-logo {
    width: 100px;
    height: 100px;
    border-radius: 4px;
  }

  .shop-info {
    flex: 1;
    margin-left: 15px;
  }

  .shop-name {
    font-size: 18px;
    font-weight: bold;
  }

  .shop-rating {
    display: flex;
    align-items: center;
    margin: 5px 0;
  }

  .menu-section {
    margin-top: 10px;
    background: #fff;
  }

  .section-title {
    padding: 10px 15px;
    font-weight: bold;
    border-bottom: 1px solid #f5f5f5;
  }

  .menu-category {
    padding: 0 15px;
  }

  .category-title {
    padding: 10px 0;
    font-weight: bold;
  }

  .menu-item {
    display: flex;
    padding: 10px 0;
    border-bottom: 1px solid #f5f5f5;
  }

  .item-image {
    width: 60px;
    height: 60px;
    border-radius: 4px;
  }

  .item-info {
    flex: 1;
    margin-left: 10px;
  }

  .item-name {
    font-size: 16px;
  }

  .item-desc {
    font-size: 12px;
    color: #999;
    margin: 3px 0;
  }

  .item-price {
    color: #f44;
    font-weight: bold;
  }

  /* 新增购物车相关样式 */
  .cart-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 50px;
    background: #fff;
    display: flex;
    align-items: center;
    padding: 0 15px;
    box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
  }

  .cart-icon {
    position: relative;
    margin-right: 15px;
  }

  .cart-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: #f44;
    color: #fff;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    font-size: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .cart-total {
    flex: 1;
    font-weight: bold;
  }

  .checkout-btn {
    background: #f44;
    color: #fff;
    border-radius: 20px;
    padding: 0 20px;
    height: 36px;
    line-height: 36px;
    font-size: 14px;
  }

  .cart-popup {
    background: #fff;
    max-height: 60vh;
    padding: 15px;
  }

  .popup-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 10px;
    border-bottom: 1px solid #eee;
  }

  .cart-items {
    max-height: 50vh;
    padding: 10px 0;
  }

  .cart-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    border-bottom: 1px solid #f5f5f5;
  }

  .item-actions {
    display: flex;
    align-items: center;
  }

  .btn-decrease,
  .btn-increase {
    width: 30px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #eee;
    background: #fff;
  }

  .item-quantity {
    margin: 0 10px;
  }
</style>